package com.mai.realtime.app.dwd.log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mai.realtime.app.BaseAppV1;
import com.mai.realtime.common.Constant;
import com.mai.realtime.util.AtguiguUtil;
import com.mai.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * com.atguigu.realtime.app.dwd.log.Dwd_02_DwdTrafficUniqueVisitorDetail
 * 流量域独立访客事务事实表
 */
public class Dwd_02_DwdTrafficUniqueVisitorDetail extends BaseAppV1 {
    public static void main(String[] args) {
        new Dwd_02_DwdTrafficUniqueVisitorDetail().init(3002, 2,
                "Dwd_02_DwdTrafficUniqueVisitorDetail",
                Constant.TOPIC_DWD_TRAFFIC_PAGE);
    }

    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        stream
                .map(JSON::parseObject)
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((obj, ts) -> obj.getLong("ts"))
                )
                //日志数据有三个来源web、小程序、app uid分组
                .keyBy(obj -> obj.getJSONObject("common").getString("uid"))
                //滚动窗口
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .process(new ProcessWindowFunction<JSONObject, String, String, TimeWindow>() {

                    private ValueState<String> visitDateState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        //获取运行时上下文.获取状态(值状态(名,类型))
                        //状态的结构是hashMap ，key是前面keyBy的key
                        visitDateState = getRuntimeContext().getState(new ValueStateDescriptor<String>("visitDateState", String.class));
                    }

                    @Override
                    public void process(String uid,//key
                                        Context ctx,
                                        //elements 一个窗口里的所有元素 process方法一个窗口关闭执行一次
                                        Iterable<JSONObject> elements,//(第一次访问)取最小值 转成集合 使用集合的方法
                                        Collector<String> out) throws Exception {
                        // 找到当天的第一个窗口
                        // 获取状态中的值
                        // 如果状态没值, 会返回null
                        String date = visitDateState.value();
                        //获取当天（当前窗口）时间
                        String today = AtguiguUtil.toDate(ctx.window().getStart());
                        //事件时间
                        if (!today.equals(date)) {
                            List<JSONObject> list = AtguiguUtil.toList(elements);
                            //去重需求
                            //取集合中的最小值->Collections.min(集合,比较器);
                            //JSONObject min = Collections.min(list, (o1, o2) -> o1.getLong("ts").compareTo(o2.getLong("ts")));
                            JSONObject min = Collections.min(list, Comparator.comparing(o -> o.getLong("ts")));

                            out.collect(min.toJSONString());

                            visitDateState.update(today);//更新状态
                        }
                    }
                })
                //.print();
                .addSink(FlinkSinkUtil.getKafkaSink(Constant.TOPIC_DWD_TRAFFIC_UV_DETAIL));

    }
}
